package cyou.breathe.tools;


/**
 * @author: breathe
 * @createTime: 2025-10-31
 */


import org.jetbrains.annotations.NotNull;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * obsidian 迁移
 * @author: breathe
 * @createTime: 2025-10-24
 */
public class PhotoMoveService {

    private static final String FROM_PATH = "/Users/breathe/Downloads/breathe-note-java/photo";

    private static final String TO_PATH = "/Users/breathe/Downloads/breathe-note-own";

    private static final String PHOTO_SUFFIX = "photo";

    private static final Pattern PHOTO_PATTERN = Pattern.compile("!\\[\\[([\\w\\s\\.\\-]+)\\]\\]");

    public static void main(String[] args) throws IOException {
        Path startPath = Paths.get(TO_PATH);

        // 存储图片名字
        List<String> photoNames = new ArrayList<>();

        // 遍历文件夹
        Files.walkFileTree(startPath, new SimpleFileVisitor<>() {
            @Override
            public @NotNull FileVisitResult visitFile(@NotNull Path file, @NotNull BasicFileAttributes attrs) throws IOException {
                // 对每个文件执行操作
                if (!file.toString().endsWith(".md")) {
                    return FileVisitResult.CONTINUE;
                }

                try (FileInputStream fis = new FileInputStream(file.toFile())) {
                    String content = new String(fis.readAllBytes());
                    // 匹配图片名字
                    Matcher matcher = PHOTO_PATTERN.matcher(content);
                    while (matcher.find()) {
                        // 获取匹配到的图片名字
                        photoNames.add(matcher.group(1));
                    }
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public @NotNull FileVisitResult visitFileFailed(@NotNull Path file, @NotNull IOException exc) throws IOException {
                // 如果访问文件失败，继续遍历
                return FileVisitResult.CONTINUE;
            }

            @Override
            public @NotNull FileVisitResult postVisitDirectory(@NotNull Path dir, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public @NotNull FileVisitResult preVisitDirectory(@NotNull Path dir, @NotNull BasicFileAttributes attrs) throws IOException {
                if ("photo".equals(dir.getFileName().toString()) ||
                        ".obsidian".equals(dir.getFileName().toString()) ||
                        ".DS_Store".equals(dir.getFileName().toString())) {
                    return FileVisitResult.SKIP_SUBTREE;
                }
                return FileVisitResult.CONTINUE;
            }
        });

        // 移动图片
        movePhotos(photoNames);
    }

    private static void movePhotos(List<String> photoNames) throws IOException {
        // 传输照片的双方
        Path from = Paths.get(FROM_PATH);
        Path to = Paths.get(TO_PATH + "/" + PHOTO_SUFFIX);
        List<String> notFoundPhotos = new ArrayList<>();

        for (String photoName : photoNames) {
            Path photoPath = from.resolve(photoName);
            if (Files.exists(photoPath)) {
                Files.createDirectories(to);
                Files.copy(photoPath, to.resolve(photoName), StandardCopyOption.REPLACE_EXISTING);
            } else {
                notFoundPhotos.add(photoName);
            }
        }

        if (!notFoundPhotos.isEmpty()) {
            System.out.println("以下照片没有被发现");
            for (String notFoundPhoto : notFoundPhotos) {
                System.out.println(notFoundPhoto);
            }
        }

    }
}
